package edu.yctc.weblog.controller.rest.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import edu.yctc.weblog.constant.ConstantHolder;
import edu.yctc.weblog.constant.ResultCode;
import edu.yctc.weblog.constant.URLConstant;
import edu.yctc.weblog.controller.rest.ConfigRestController;
import edu.yctc.weblog.entity.ResultDO;
import edu.yctc.weblog.service.ConfigService;
import edu.yctc.weblog.vo.FooterConfigVO;
import edu.yctc.weblog.vo.UserInfoVO;
import edu.yctc.weblog.vo.WebsiteConfigVO;

@RestController
@RequestMapping(URLConstant.S + URLConstant.CONFIG)
public class ConfigRestControllerImpl implements ConfigRestController {

    @Autowired
    private ConfigService configService;

    @Override
    @PostMapping(URLConstant.CONFIG_WEBSITE_CONFIG)
    public ResultDO<Void> websiteConfig(@ModelAttribute WebsiteConfigVO websiteConfigVO) {
        int updateResult = 0;
        if (StringUtils.isBlank(websiteConfigVO.getWebsiteName()) == false) {
            updateResult += configService
                .updateConfig(ConstantHolder.CONFIG_WEBSITE_NAME, websiteConfigVO.getWebsiteName()).getModule();
        }
        if (StringUtils.isBlank(websiteConfigVO.getWebsiteDescription()) == false) {
            updateResult += configService
                .updateConfig(ConstantHolder.CONFIG_WEBSITE_DESCRIPTION, websiteConfigVO.getWebsiteDescription())
                .getModule();
        }
        if (StringUtils.isBlank(websiteConfigVO.getWebsiteLogo()) == false) {
            updateResult += configService
                .updateConfig(ConstantHolder.CONFIG_WEBSITE_LOGO, websiteConfigVO.getWebsiteLogo()).getModule();
        }
        if (StringUtils.isBlank(websiteConfigVO.getWebsiteIcon()) == false) {
            updateResult += configService
                .updateConfig(ConstantHolder.CONFIG_WEBSITE_ICON, websiteConfigVO.getWebsiteIcon()).getModule();
        }
        if (updateResult > 0) {
            return new ResultDO<>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
        } else {
            return new ResultDO<>(false, ResultCode.DB_ERROR, ResultCode.MSG_DB_ERROR);
        }
    }

    @Override
    @PostMapping(URLConstant.CONFIG_USER_INFO)
    public ResultDO<Void> userInfo(@ModelAttribute UserInfoVO userInfoVO) {
        int updateResult = 0;
        if (StringUtils.isBlank(userInfoVO.getYourAvatar()) == false) {
            updateResult +=
                configService.updateConfig(ConstantHolder.USER_INFO_AVATAR, userInfoVO.getYourAvatar()).getModule();
        }
        if (StringUtils.isBlank(userInfoVO.getYourName()) == false) {
            updateResult +=
                configService.updateConfig(ConstantHolder.USER_INFO_NAME, userInfoVO.getYourName()).getModule();
        }
        if (StringUtils.isBlank(userInfoVO.getYourEmail()) == false) {
            updateResult +=
                configService.updateConfig(ConstantHolder.USER_INFO_EMAIL, userInfoVO.getYourEmail()).getModule();
        }
        if (updateResult > 0) {
            return new ResultDO<>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
        } else {
            return new ResultDO<>(false, ResultCode.DB_ERROR, ResultCode.MSG_DB_ERROR);
        }
    }

    @Override
    @PostMapping(URLConstant.CONFIG_FOOTER_CONFIG)
    public ResultDO<Void> footerConfig(@ModelAttribute FooterConfigVO footerConfigVO) {
        int updateResult = 0;
        if (StringUtils.isBlank(footerConfigVO.getFooterAbout()) == false) {
            updateResult += configService
                .updateConfig(ConstantHolder.FOOTER_CONFIG_ABOUT, footerConfigVO.getFooterAbout()).getModule();
        }
        if (StringUtils.isBlank(footerConfigVO.getFooterICP()) == false) {
            updateResult +=
                configService.updateConfig(ConstantHolder.FOOTER_CONFIG_ICP, footerConfigVO.getFooterICP()).getModule();
        }
        if (StringUtils.isBlank(footerConfigVO.getFooterCopyRight()) == false) {
            updateResult += configService
                .updateConfig(ConstantHolder.FOOTER_CONFIG_POWERRD_BY, footerConfigVO.getFooterPoweredBy()).getModule();
        }
        if (StringUtils.isBlank(footerConfigVO.getFooterPoweredByURL()) == false) {
            updateResult += configService
                .updateConfig(ConstantHolder.FOOTER_CONFIG_POWERRD_BY_URL, footerConfigVO.getFooterPoweredByURL())
                .getModule();
        }
        if (updateResult > 0) {
            return new ResultDO<>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
        } else {
            return new ResultDO<>(false, ResultCode.DB_ERROR, ResultCode.MSG_DB_ERROR);
        }
    }

}
